home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 8: LINUX Games / Linux Cubed Series 8 - LINUX Games.iso / games / video / fly8111-.000 / fly8111- / fly8 / sky.c < prev    next >
C/C++ Source or Header  |  1979-12-31  |  4KB  |  199 lines

  1. /* --------------------------------- sky.c ---------------------------------- */
  2.  
  3. /* This is part of the flight simulator 'fly8'.
  4.  * Author: Eyal Lebedinsky (eyal@ise.canberra.edu.au).
  5. */
  6.  
  7. /* Render the sky algorithmically.
  8. */
  9.  
  10. #include "fly.h"
  11.  
  12.  
  13. #define NSIZE    (st.SkyLines*2+1)
  14.  
  15. static ANGLE    *skylines = 0;
  16.  
  17. extern int FAR
  18. sky_init (void)
  19. {
  20.     int    i;
  21.     ANGLE    a, v;
  22.  
  23.     if (skylines)
  24.         sky_term ();
  25.  
  26.     skylines = (ANGLE *) memory_alloc (NSIZE * sizeof (*skylines));
  27.     if (!skylines) {
  28.         MsgEPrintf (-50, "No Sky");
  29.         st.flags &= ~SF_SKY;
  30.         return (1);
  31.     }
  32.     skylines[0] = 0;
  33.     skylines[st.SkyLines] = D90;
  34.     skylines[2*st.SkyLines] = D180;
  35.  
  36.     for (i = 1; i < st.SkyLines; ++i) {
  37.         a = muldiv (i, D90, st.SkyLines);
  38.         v = fmul (D90, SIN (a));
  39.         skylines[st.SkyLines-i] = D90 - v;
  40.         skylines[st.SkyLines+i] = D90 + v;
  41.     }
  42.     return (0);
  43. }
  44.  
  45. extern void FAR
  46. sky_term (void)
  47. {
  48.     if (!skylines)
  49.         return;
  50.  
  51.     memory_free (skylines, NSIZE * sizeof (*skylines));
  52.     skylines = 0;
  53. }
  54.  
  55. extern void FAR
  56. show_sky (VIEW *view, OBJECT *p)
  57. {
  58.     int    orgx, orgy, maxx, maxy, sizex, sizey;
  59.     int    sroll, croll, srollh, crollh, spitch, cpitch;
  60.     int    h, xh, yh;
  61.     int    i, k, n, x[2], y[2], slope;
  62.     ANGLE    a, z;
  63.     AVECT    aa;
  64.     MAT    T;
  65.  
  66.     if (!skylines || !(st.flags & SF_SKY))
  67.         return;
  68.  
  69.     get_area (view, &orgx, &orgy, &sizex, &sizey);
  70.  
  71.     maxx = VP->maxx;
  72.     maxy = VP->maxy;
  73.  
  74.     if (maxx < (i = (sizex+1) << 1))
  75.         maxx = i;
  76.     if (maxy < (i = (sizey+1) << 1))
  77.         maxy = i;
  78.  
  79.     Mident (T);
  80.     if (VP->roty)
  81.         Mroty (T, -VP->roty);
  82.     if (VP->rotx)
  83.         Mrotx (T, -VP->rotx);
  84.     if (VP->rotz)
  85.         Mrotz (T, -VP->rotz);
  86.     fMroty (T, p->siny, p->cosy);
  87.     fMrotx (T, p->sinx, p->cosx);
  88.     fMrotz (T, p->sinz, p->cosz);
  89.     Mangles (0, T, aa, 0);
  90.  
  91.     i = ihypot2d (maxx, maxy);
  92.     a = ATAN (i, VP->z);
  93.  
  94.     i = aa[X] - a;
  95.     for (n = 0; n < NSIZE; ++n)
  96.         if (skylines[n] >= i)
  97.             break;
  98. /*    a = a/2*3;*/
  99.  
  100.     sroll  = SIN (-aa[Y]);
  101.     croll  = COS (-aa[Y]);
  102.  
  103.     srollh = muldiv (sroll, sizex, maxx);
  104.     crollh = muldiv (croll, sizey, maxy);
  105.  
  106.     slope = iabs(fmul (croll, sizey)) > iabs(fmul (sroll, sizex));
  107.  
  108.     sroll  = muldiv (sroll, sizey, maxy);
  109.     croll  = muldiv (croll, sizex, maxx);
  110.  
  111.     gr_color (ST_SKY);
  112.  
  113.     for (; n < NSIZE; ++n) {
  114.     z = skylines[n]            /* angle being shown */
  115.         -aa[X];            /* relative pitch of line */
  116.     if (iabs (z) > (Uint)a)        /* trivial reject */
  117.         break;
  118.  
  119.     spitch = SIN (z);
  120.     cpitch = COS (z);
  121.  
  122.     h = muldiv (VP->z, spitch, cpitch);
  123.  
  124.     xh = fmul (h, srollh);
  125.     yh = fmul (h, crollh);
  126.  
  127.     if (!croll) {
  128.         if (xh > sizex || xh < -sizex)
  129.             continue;
  130.         x[0] = xh;
  131.         y[0] = sizey;
  132.         x[1] = xh;
  133.         y[1] = -sizey;
  134.     } else if (!sroll) {
  135.         if (yh > sizey || yh < -sizey)
  136.             continue;
  137.         x[0] = sizex;
  138.         y[0] = yh;
  139.         x[1] = -sizex;
  140.         y[1] = yh;
  141.     } else {
  142.         for (i = 0, k = 0; k < 2; ++k) {
  143.             if (slope^k) {
  144.                 if (!k || xh >= 0) {
  145.                     x[i] = sizex;
  146.                     y[i] = yh-muldiv (x[i]-xh, sroll,
  147.                                 croll);
  148.                     if (y[i] <= sizey && y[i] >= -sizey)
  149.                         if (++i >= 2)
  150.                             break;
  151.                 }
  152.                 if (!k || xh < 0) {
  153.                     x[i] = -sizex;
  154.                     y[i] = yh-muldiv (x[i]-xh, sroll,
  155.                                 croll);
  156.                     if (y[i] <= sizey && y[i] >= -sizey)
  157.                         if (++i >= 2)
  158.                             break;
  159.                 }
  160.                 if (0 == i)
  161.                     break;
  162.             } else {
  163.                 if (!k || yh >= 0) {
  164.                     y[i] = sizey;
  165.                     x[i] = xh-muldiv (y[i]-yh, croll,
  166.                                 sroll);
  167.                     if (x[i] <= sizex && x[i] >= -sizex)
  168.                         if (++i >= 2)
  169.                                 break;
  170.                 }
  171.                 if (!k || yh < 0) {
  172.                     y[i] = -sizey;
  173.                     x[i] = xh-muldiv (y[i]-yh, croll,
  174.                             sroll);
  175.                     if (x[i] <= sizex && x[i] >= -sizex)
  176.                         if (++i >= 2)
  177.                             break;
  178.                 }
  179.                 if (0 == i)
  180.                     break;
  181.             }
  182.         }
  183.         if (i < 2)
  184.             continue;
  185.     }
  186.  
  187. /* a mirror has left-right swapped.
  188. */
  189.     if (VP->flags & VF_MIRROR) {
  190.         x[0] = -x[0];
  191.         x[1] = -x[1];
  192.     }
  193.  
  194.     gr_move (orgx+x[0], orgy-y[0]);
  195.     gr_draw (orgx+x[1], orgy-y[1]);
  196.     }
  197. }
  198. #undef NSIZE
  199.